package com.houtai.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houtai.commonutils.Result;
import com.houtai.eduservice.config.OperationLogDesc;
import com.houtai.eduservice.entity.StuRead;
import com.houtai.eduservice.entity.vo.ReadQuery;
import com.houtai.eduservice.service.StuReadService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-12-14
 */
@RestController
@RequestMapping("/eduservice/sturead")
public class StuReadController {

    @Autowired
    public StuReadService stuReadService;
    //添加
    @PostMapping("addRead")
    @ApiOperation(value = "添加绘本")
    @OperationLogDesc(module = "学习资源模块",description = "添加绘本")
    public Result addRead(@RequestBody @Valid StuRead stuRead){
        stuReadService.save(stuRead);
        return Result.ok();
    }

    @ApiOperation(value = "多条件组合查询带分页")
    @PostMapping("pageReadCondition/{current}/{limit}")
    @OperationLogDesc(module = "课程资源模块",description = "查看绘本列表")
    public Result pageVideoCondition(@PathVariable long current,
                                     @PathVariable long limit,
                                     @RequestBody(required = false) ReadQuery readQuery){
        //创建page对象
        Page<StuRead> pageT = new Page<>(current,limit);
        //调用方法实现分页
        //构建条件
        QueryWrapper<StuRead> wrapper=new QueryWrapper<>();
        //wrapper
        //动态sql
        String title = readQuery.getTitle();
        String begin = readQuery.getBegin();
        String end = readQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(title)){
            //构建条件
            wrapper.like("title",title);

        }
        if(!StringUtils.isEmpty(begin)){
            //构建条件
            wrapper.gt("gmt_create",begin);

        }
        if(!StringUtils.isEmpty(end)){
            //构建条件
            wrapper.le("gmt_create",end);

        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        stuReadService.page(pageT,wrapper);

        long total = pageT.getTotal();//总记录数
        List<StuRead> records = pageT.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);

    }

    //修改小节
    //根据id查询
    @GetMapping("getInfo/{readId}")
    @ApiOperation(value = "获取信息")
    @OperationLogDesc(module = "学习资源模块",description = "获取绘本信息")
    public Result getInfo(@PathVariable Integer readId){
        StuRead stuRead=stuReadService.getById(readId);
        return Result.ok().data("read",stuRead);
    }
    @PostMapping("update")
    @ApiOperation(value = "修改")
    @OperationLogDesc(module = "学习资源模块",description = "修改绘本信息")
    public Result update(@RequestBody @Valid StuRead stuRead){
        stuReadService.updateById(stuRead);
        return Result.ok();
    }

    @ApiOperation(value = "逻辑删除")
    @DeleteMapping("{id}")
    @OperationLogDesc(module = "学习资源模块",description = "删除绘本")
    public Result removeT(@ApiParam(name="id",value = "ID",required = true) @PathVariable int id){
        boolean flag=stuReadService.removeById(id);
        if(flag){
            return Result.ok();
        }
        else {
            return Result.error();
        }

    }

}

